<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/StatusRoster.class.php";
require_once dirname(__FILE__)."/Localization.class.php";
require_once dirname(__FILE__)."/Log.class.php";
require_once dirname(__FILE__)."/Utf8.class.php";

class File
{
	static private $instance;
	
	private $file_data;
	
	private $status_roster;
	private $localization;
	private $log;
	
	protected function __construct()
	{
		$this->file_data = &$_FILES;
		
		$this->status_roster = StatusRoster::GetInstance();
		$this->localization = Localization::GetInstance();
		$this->log = new Log('file');
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new File();
		}
		
		return self::$instance;
	}
	
	function HasFile($field)
	{
		if ( array_key_exists($field, $this->file_data) )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	function CheckFileStatus($field)
	{
		if ($this->file_data[$field]["error"] != UPLOAD_ERR_OK)
		{
			switch ($this->file_data[$field]["error"])
			{
			case UPLOAD_ERR_INI_SIZE:
				$this->status_roster->AddError( $this->localization->Get('UPLOAD_ERR_INI_SIZE') );
				$this->log->AddNote("A user tried to upload a file with a size greater than the one specified by the upload_max_filesize directive in php.ini.");
				return "100";
			case UPLOAD_ERR_FORM_SIZE:
				$this->status_roster->AddError( $this->localization->Get('UPLOAD_ERR_FORM_SIZE') );
				$this->log->AddNote("A user tried to upload a file with a size greater than the one specified in the upload form of the roster.");
				return "101";
			case UPLOAD_ERR_PARTIAL:
				$this->status_roster->AddError( $this->localization->Get('UPLOAD_ERR_PARTIAL') );
				return "102";
			case UPLOAD_ERR_NO_FILE:
				$this->status_roster->AddError( $this->localization->Get('UPLOAD_ERR_NO_FILE') );
				return "103";
			case UPLOAD_ERR_NO_TMP_DIR:
				$this->status_roster->AddError( $this->localization->Get('UPLOAD_ERR_NO_TMP_DIR') );
				$this->log->AddNote("The server produced an UPLOAD_ERR_NO_TMP_DIR error when user tried to upload a file. Please check configuration.");
				return "104";
			case UPLOAD_ERR_CANT_WRITE:
				$this->status_roster->AddError( $this->localization->Get('UPLOAD_ERR_CANT_WRITE') );
				$this->log->AddNote("The server could not write to the disk when user tried to upload a file.");
				return "105";
			case UPLOAD_ERR_EXTENSION:
				$this->status_roster->AddError( $this->localization->Get('UPLOAD_ERR_EXTENSION') );
				$this->log->AddNote("A user tried to upload a file with a forbidden extension.");
				return "106";
			}
		}
		else
		{
			return "OK";
		}
	}
	
	function GetFileName($field)
	{
		return $this->file_data[$field]["name"];
	}
	
	function GetFileContents($field)
	{
		$t_file = file( $this->file_data[$field]["tmp_name"] );
		
		foreach($t_file as &$line)
		{
			if ( Utf8::is_utf8($line) )
			{
				$line = utf8_decode($line);
			}
			
			$line = str_replace("<", "", $line);
			$line = str_replace("&lt;", "", $line);
			$line = str_replace(">", "", $line);
			$line = str_replace("&gt;", "", $line);
		}
		
		return $t_file;
	}
}

?>